﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            for (int i = 1; i <= 31; i++)
            {
                dlstNgay.Items.Add(i.ToString());
            }
            for (int i = 1; i <= 12; i++)
            {
                dlstThang.Items.Add(i.ToString());
            }
            for (int i = int.Parse(DateTime.Now.Year.ToString()) - 70; i <= int.Parse(DateTime.Now.Year.ToString()) - 10; i++)
            {
                dlstNam.Items.Add(i.ToString());
            }
            if (Session["Username"] != null)
            {
                String username = Session["UserName"].ToString();
                ChoDienTuDataContext cdt = new ChoDienTuDataContext();
                var user = cdt.Select_CustomerByUsername(username).First();
                
                txtUserName.Text = user.Username; txtUserName.ReadOnly = true;
                this.txtPassword.Text = user.Password;
             
                txtFullName.Text = user.FullName;

                //////////////////

                txtAddress.Text = user.Address;
                txtEmail.Text = user.Email;

                btnSend.Text = "Update";
            }
        }

    }


    bool bNgaySinh = false;
    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {

        int iNgay = int.Parse(dlstNgay.SelectedValue);
        int iThang = int.Parse(dlstThang.SelectedValue);
        int iNam = int.Parse(args.Value);

        Cngay cn = new Cngay(iNgay, iThang, iNam);
        bNgaySinh = cn.XetHopLe();
        args.IsValid = bNgaySinh;
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        String username = txtUserName.Text;
        String email = txtEmail.Text;
        String address = txtAddress.Text;
        String password = txtPassword.Text;
        String name = txtFullName.Text;
        int bank = int.Parse(txtBank.Text);
        DateTime bd = DateTime.Now;
        ChoDienTuDataContext cdt = new ChoDienTuDataContext();
        if (btnSend.Text == "Submit")
        {
            cdt.Insert_Customer(username, password, bd, name, email, address, 1, "", bank);
            cdt.SubmitChanges();
            lblCheck.Text = "success";
        }
        else
        {
            var user = cdt.Select_CustomerByUsername(Session["UserName"].ToString()).First();
            cdt.Update_Customer(user.ID, name, email, address);
            cdt.SubmitChanges();
        }

    }
   
    protected void txtUserName_TextChanged(object sender, EventArgs e)
    {
        if (txtUserName.Text != "")
        {
            ChoDienTuDataContext cdt = new ChoDienTuDataContext();
            int num = cdt.Select_CustomerByUsername(txtUserName.Text).Count();
           
            if (num > 0)
            {
               
                lblCheck.ForeColor = System.Drawing.Color.Red;
                lblCheck.Text = "Username đã tồn tại";
            }
            else
            {
                lblCheck.ForeColor = System.Drawing.Color.Blue;
                lblCheck.Text = "Username có thể sử dụng";
            }

        }
        else
        {
            lblCheck.ForeColor = System.Drawing.Color.Red;
            lblCheck.Text = "Chưa nhập Username";
        }
    }
}